#include"login.h"
using namespace std;


int enroll(string ID, string name, string password, bool isadmin) {         //注册账户,返回1表示注册成功，返回0表示ID重复,-1表示ID不合要求（ID要求10位纯数字）
	if (ID.size() != 10) { return -1; }
	for (int i = 0; i < 9; i++) {
		if (ID[i] < '0' || ID[i]>'9') {
			return -1;
		}
	}
	if (Looking_forward.find(ID) != Looking_forward.end()) {
		return 0;
	}
	if (isadmin == 0) {
		User* p = new User(ID, name, password);
		Looking_forward[ID] = *p;
		UserKu.push_back(*p);
		return 1;
	}
	else {
		Admin* p = new Admin(ID, name, password);
		Looking_forward[ID] = *p;
		AdminKu.push_back(*p);
		AdminKu.push_back(*p);
		return 1;
	}
}

string Login(string ID, string password, bool isadmin) {              //登录账户,返回ID，如果登录失败返回登录失败信息
	if (Looking_forward.find(ID) == Looking_forward.end()) {
		return "Can't find ID";   //找不到用户名
	}
	Person* p = &Looking_forward[ID];
	if (isadmin == 1) {
		if (p->type != 2) {
			return "Can't find ID";   //找不到用户名
		}
		if (p->Getpassword() != password) {
			return "Wrong password";   //密码错误
		}
		else {
			return ID;
		}
	}
	else {
		if (p->type != 1) {
			return "Can't find ID";   //找不到用户名
		}
		if (p->Getpassword() != password) {
			return "Wrong password";   //密码错误
		}
		else {
			return ID;
		}
	}
}

//bool Logout(string ID) {      //注销账号
//	map<string, Person*>::iterator it = Looking_forward.begin();
//	Person* p;
//	for (; it != Looking_forward.end(); it++) {
//		if (it->first == ID) {
//			p = it->second;
//			Looking_forward.erase(it);    //删除查找map相应元素
//			break;
//		}
//	}
//	if (it == Looking_forward.end()) {
//		return 0;  //所注销的账号不存在
//	}
//	vector<Person*>::iterator it2 = Ku.begin();
//	for (; it2 != Ku.end(); it++) {
//		if (*it2 == p) {
//			p->~Person();      //析构
//			Ku.erase(it2);
//			return 1;
//		}
//	}
//	return 0;   //程序故障，理论不可能查找表中有库中无
//}